package com.Controller;

import com.pojo.Role;
import com.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list")
    public String list(Model model){
        List<Role> list = roleService.list();
        model.addAttribute("list",list);
        return "sysRole/list";
    }
    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "sysRole/add";
    }

    @RequestMapping(value = "/add")
    public String add(Role role){
        int result = roleService.add(role);
        if (result > 0){
            return "redirect:/role/list";
        }
        return "role/toAdd";
    }

    @RequestMapping(value = "/selectId")
    public String selectId(Model model,int id){
        Role role = roleService.selectId(id);
        model.addAttribute("role",role);
        return "sysRole/update";
    }

    @RequestMapping(value = "/update")
    public String update(Role role){
        int result = roleService.update(role);
        if (result > 0){
            return "redirect:/role/list";
        }
        return "role/selectId";
    }

    @RequestMapping(value = "/delete")
    public String delete(int id){
        int result = roleService.delete(id);
        if (result > 0){
            return "sysRole/list";
        }
        return "sysRole/list";
    }
}
